package com.airsystem.pos.bengkel.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import com.airsystem.pos.bengkel.controller.MasterBillingDetailController;
import com.airsystem.pos.bengkel.controller.MenuController;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterBillingDetail extends MasterBillingDetailController {

	public MasterBillingDetail(MenuController menuController) {
		super();

		this.menuController = menuController;
		initComponents();
		setup();
		load();
	}

	private void initComponents() {
		setTitle("Data Tagihan Bulanan");
		setFrameIcon(new ImageIcon(getClass().getResource("/images/walletx.png")));
		setIconifiable(true);
		setClosable(true);

		baseLayout.columnWidths = new int[] { 700 };
		baseLayout.rowHeights = new int[] { 0, 0, 0 };
		baseLayout.columnWeights = new double[] { 0.0 };
		baseLayout.rowWeights = new double[] { 0.0, 1.0, 0.0 };
		getContentPane().setLayout(baseLayout);

		panelLayout.columnWidths = new int[] { 0, 200, 200, 150 };
		panelLayout.rowHeights = new int[] { 0, 0, 0 };
		panelLayout.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0 };
		panelLayout.rowWeights = new double[] { 0.0, 0.0, 0.0 };
		basePanel.setLayout(panelLayout);

		periodeLabel.setFont(new Font("Verdana", 0, 12));
		periodeLabel.setText("Periode Tagihan");

		jenisLabel.setFont(new Font("Verdana", 0, 12));
		jenisLabel.setText("Jenis Tagihan");

		biayaLabel.setFont(new Font("Verdana", 0, 12));
		biayaLabel.setText("Biaya Tagihan/bln");

		bulanCombo.setFont(new Font("Verdana", 0, 12));

		tahunCombo.setFont(new Font("Verdana", 0, 12));

		jenisCombo.setFont(new Font("Verdana", 0, 12));

		biayaField.setFont(new Font("Verdana", 0, 12));
		biayaField.setColumns(15);
		biayaField.setDocument(new TextUtil(17, TextUtil.NUMBER));

		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));

		searchButton.setIcon(new ImageIcon(getClass().getResource("/images/search.png")));
		searchButton.setFont(new Font("Verdana", 1, 12));
		searchButton.setText("CARI");

		addButton.setIcon(new ImageIcon(getClass().getResource("/images/add.png")));
		addButton.setFont(new Font("Verdana", 1, 12));
		addButton.setText("TAMBAH");
		buttonPanel.add(addButton);

		saveButton.setIcon(new ImageIcon(getClass().getResource("/images/save.png")));
		saveButton.setFont(new Font("Verdana", 1, 12));
		saveButton.setText("SIMPAN");
		buttonPanel.add(saveButton);

		updateButton.setIcon(new ImageIcon(getClass().getResource("/images/update.png")));
		updateButton.setFont(new Font("Verdana", 1, 12));
		updateButton.setText("UBAH");
		buttonPanel.add(updateButton);

		deleteButton.setIcon(new ImageIcon(getClass().getResource("/images/delete.png")));
		deleteButton.setFont(new Font("Verdana", 1, 12));
		deleteButton.setText("HAPUS");
		buttonPanel.add(deleteButton);

		cancelButton.setIcon(new ImageIcon(getClass().getResource("/images/cancel.png")));
		cancelButton.setFont(new Font("Verdana", 1, 12));
		cancelButton.setText("BATAL");
		buttonPanel.add(cancelButton);

		initBillings();

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});

		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});

		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (saveButton.isVisible()) {
					editButtonActionPerformed(evt);
				} else {
					updateButtonActionPerformed(evt);
				}
			}
		});

		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (saveButton.isVisible()) {
					removeButtonActionPerformed(evt);
				} else {
					deleteButtonActionPerformed(evt);
				}
			}
		});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		basePanel.add(periodeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 30, 15, 0), 0, 0));
		basePanel.add(jenisLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 30, 15, 0), 0, 0));
		basePanel.add(biayaLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 30, 15, 0), 0, 0));
		basePanel.add(bulanCombo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30, 15, 15, 10), 0, 0));
		basePanel.add(tahunCombo, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30, 15, 15, 10), 0, 0));
		basePanel.add(jenisCombo, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 15, 15, 10), 0, 0));
		basePanel.add(biayaField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 15, 15, 10), 0, 0));
		basePanel.add(searchButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30, 15, 15, 0), 0, 0));
		basePanel.add(buttonPanel, new GridBagConstraints(0, 3, 4, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		getContentPane().add(basePanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 700) / 2, (screenSize.height - 700) / 2, 700, 500);
	}

	private void initBillings() {
		tablebillingScroll.setViewportView(tableTagihan);
		tableTagihan.setFont(new Font("Verdana", 0, 12));
		tableTagihan.setModel(tableModel);

		tableTagihan.getTableHeader().setReorderingAllowed(false);
		tableTagihan.getColumnModel().getColumn(0).setPreferredWidth(200);
		tableTagihan.getColumnModel().getColumn(1).setPreferredWidth(100);

		tableTagihan.setPreferredScrollableViewportSize(new Dimension(150, 150));
		tableTagihan.setFillsViewportHeight(true);

		tableTagihan.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				tableBillingMouseClicked(evt);
			}
		});

		fieldPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));

		totalLabel.setFont(new Font("Verdana", 0, 12));
		totalLabel.setText("Total Tagihan/bln");
		fieldPanel.add(totalLabel);

		totalField.setFont(new Font("Verdana", 0, 12));
		totalField.setColumns(15);
		fieldPanel.add(totalField);

		getContentPane().add(tablebillingScroll, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 1, 1, 1), 0, 0));
		getContentPane().add(fieldPanel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 300), 0, 0));
	}

	private void searchButtonActionPerformed(ActionEvent evt) {
		search();
	}

	private void tableBillingMouseClicked(MouseEvent evt) {
		row = tableTagihan.getSelectedRow();

		if (evt.getClickCount() == 1 && row != -1) {
			init();
		}
	}

	private void addButtonActionPerformed(ActionEvent evt) {
		add();
	}

	private void editButtonActionPerformed(ActionEvent evt) {
		edit();
	}

	private void removeButtonActionPerformed(ActionEvent evt) {
		remove();
	}

	private void saveButtonActionPerformed(ActionEvent evt) {
		save();
	}

	private void updateButtonActionPerformed(ActionEvent evt) {
		update();
	}

	private void deleteButtonActionPerformed(ActionEvent evt) {
		delete();
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		cancel();
	}
}